﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;

namespace Gaea.CodeFirst.SqlServer
{
    public class SqlServerOperateTable : ITable
    {
        public async Task CreateTable<T>(OperateTableContext operateTableContext) where T : class
        {
            await CreateTable(operateTableContext);
        }

        public async Task AlterTable<T>(OperateTableContext operateTableContext) where T : class
        {
            await AlterTable(operateTableContext);
        }
        public async Task CreateTable(OperateTableContext operateTableContext)
        {
            var sql = "";

            CreateTableBasic createTableBasic = new CreateTableBasic();
            var ColumnSql = createTableBasic.BasicCreateTable(operateTableContext);

            sql = "CREATE TABLE " + operateTableContext.TableName + " ( " + ColumnSql + " );";
            await operateTableContext.gaeaBasicPower.ExecuteAsync(sql, null);
            sql = "alter table " + operateTableContext.TableName + " drop column Id;";
            await operateTableContext.gaeaBasicPower.ExecuteAsync(sql, null);
            sql = "alter table " + operateTableContext.TableName + " add Id int not null primary key Identity(1,1);";
            await operateTableContext.gaeaBasicPower.ExecuteAsync(sql, null);
        }

        public async Task AlterTable(OperateTableContext operateTableContext)
        {
            PropertyInfo[] propertyInfo = operateTableContext.typeOfGaeaSon.GetProperties();
            //string ColumnSql = "";
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();

            var tableName = operateTableContext.TableName.TrimStart('[');
            tableName = tableName.TrimEnd(']');
            var allColumns = await operateTableContext.gaeaBasicPower.FindAsync<INFORMATION_SCHEMA_Columns>("SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='" + tableName + "'", new { });
            operateTableContext.allSqlServerColumns = allColumns;
            AlterTableBasic alterTableBasic = new AlterTableBasic();
            alterTableBasic.BasicAlterTable(operateTableContext);
            //var enumerator = allColumns.GetEnumerator();
            //while (enumerator.MoveNext())
            //{
            //    string ColumnName = enumerator.Current.COLUMN_NAME;
            //    string ColumnType = enumerator.Current.DATA_TYPE;
            //    if (ColumnType == "varchar")
            //    {
            //        ColumnType = enumerator.Current.DATA_TYPE + "(" + enumerator.Current.CHARACTER_MAXIMUM_LENGTH + ")";
            //    }
            //    keyValuePairs.Add(ColumnName, ColumnType);    //将数据库表所有字段名及类型存入字典
            //}

            //foreach (var property in propertyInfo)
            //{
            //    var column_name = property.Name;
            //    var column_type = property.PropertyType.Name;

            //    var attributes = property.GetCustomAttributes();
            //    ConvertType convertType = new ConvertType();
            //    column_type = convertType.ConvertSqlServerType(attributes, column_type);

            //    //与字典内数据对比，将没有的字段在数据表内新增
            //    if (keyValuePairs.ContainsKey(column_name) == false)
            //    {
            //        if (column_name == "Id")
            //        {
            //            var sql = "alter table " + operateTableContext.TableName + " add Id int not null primary key Identity(1,1);";
            //            await operateTableContext.gaeaBasicPower.ExecuteAsync(sql, null);
            //        }
            //        else
            //        {
            //            ColumnSql = column_name + " " + column_type + " not null";
            //            var sql = "Alter TABLE " + operateTableContext.TableName + " Add " + ColumnSql + " ;";
            //            await operateTableContext.gaeaBasicPower.ExecuteAsync(sql, null);
            //        }
            //    }
            //    //包含该字段名，但类型不同的报错
            //    else if (keyValuePairs.ContainsKey(column_name) == true && keyValuePairs[column_name] != column_type.ToLower() && column_name != "Id")
            //    {
            //        //throw new Exception("This field already exists,and type is different!");
            //        Console.WriteLine(column_name +" is already exists,Db Type is "+ keyValuePairs[column_name] + " ,and code Type is "+ column_type);
            //    }
            //}
        }
    }
}
